package com.foxzzz.two.pointers;

import java.util.Stack;

public class Solution844 {
    public boolean backspaceCompare(String S, String T) {
        Stack<Character> stack = new Stack<>();
        Stack<Character> stack1 = new Stack<>();
        for (int i = 0; i < S.length(); i++) {
            if (S.charAt(i) == '#' && stack.size() != 0) {
                stack.pop();
            } else if(S.charAt(i)!= '#'){
                stack.push(S.charAt(i));
            }
        }
        for (int i = 0; i < T.length(); i++) {
            if (T.charAt(i) == '#' && stack1.size() != 0) {
                stack1.pop();
            } else if(T.charAt(i)!= '#'){
                stack1.push(T.charAt(i));
            }
        }
        if (stack.size() != stack1.size()) {
            return false;
        } else {
            for (int i = 0; i < stack.size(); i++) {
                if (!stack.pop().equals(stack1.pop())) {
                    return false;
                }
            }
        }
        return true;
    }


    public static void main(String[] args) {
        System.out.println(new Solution844().backspaceCompare("y#fo##f","y#f#o##f"));
        System.out.println(new Solution844().backspaceCompare("ab#c","ad#c"));
        System.out.println(new Solution844().backspaceCompare("ab##","c#d#"));
        System.out.println(new Solution844().backspaceCompare("","c#d#"));
        System.out.println(new Solution844().backspaceCompare("","c#"));
    }
}
